import yaml
import os
import copy

svc_dict = {}

def load_file(path: str):
    data = yaml.safe_load_all(open(path, 'rt'))
    for item in data:
        name = item['metadata']['name']
        svc_dict.setdefault(name, [])
        svc_dict[name].append(item)

load_file('../kubernetes-manifests/k8s-with-jaeger/ts-deployment-part2.yml')
load_file('../kubernetes-manifests/k8s-with-jaeger/ts-deployment-part3.yml')

for k, v in svc_dict.items():
    yaml.safe_dump_all(v, open(f'{k}.yaml', 'wt'))

skaffold_data = []
skaffold_template = yaml.safe_load(open('skaffold-template.yaml', 'rt'))

for k, v in svc_dict.items():
    cur_data = copy.deepcopy(skaffold_template)
    cur_data['metadata']['name'] = k
    cur_data['build']['artifacts'][0]['image'] = k
    cur_data['build']['artifacts'][0]['context'] = k
    cur_data['deploy']['kubectl']['manifests'][0] = f'./deployment/service-manifests/{k}.yaml'

    skaffold_data.append(cur_data)

yaml.safe_dump_all(skaffold_data, open('skaffold.yaml', 'wt'))
